package by.averianov.discounta.webapp.page.viewsale;

import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Fragment;

import by.averianov.discounta.datamodel.UserAccount;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.page.BaseLayout;
import by.averianov.discounta.webapp.page.user.UserProfilePage;
import by.averianov.discounta.webapp.page.user.userdetails.UserDetailsPanel;

public class ViewSale extends BaseLayout {
	
	private PageCreator pageCreator;
	
	public ViewSale(PageCreator pageCreator) {
		super(null);
		super.currentMenu = "viewSale";
		this.pageCreator = pageCreator;
	}
	
	@Inject
	private UserService userService;
	
	@Override
	protected void onInitialize() {
		super.onInitialize();
		//add(new UserDetailsPanel("panel-user", account, false));

		final List<UserAccount> allAccounts = userService.getAllAccountOrderId();
		
		add(new ListView<UserAccount>("details-panel", allAccounts) {
			@Override
			protected void populateItem(ListItem<UserAccount> item) {
				UserAccount account = item.getModelObject();
				item.add(new Label("id", account.getId()).setVisible(false));
				boolean isLast = allAccounts.size() - 1 == item.getIndex();
				item.add((new UserDetailsPanel("user-panel", account, isLast)));
			}
		});
		
		
		if (System.currentTimeMillis() % 2 == 0) {
			Fragment fragment = new Fragment("fragmentId", "green-fragment", ViewSale.this);
			add(fragment);
		} else {
			add(new Fragment("fragmentId", "red-fragment", ViewSale.this));
		}
		
		add(new Link("back-link") {
			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
				//setResponsePage(new HomePage());
			}
		});
		
		
	}

}